// 引用日期处理插件
import moment from "moment"; //导入文件
//  转小写
let lower = value => value.toLowerCase();
//  转大写
let upper = value => value.toUpperCase();

let currencyStyle = (value, style) => {
  //  货币格式
  /**
   * style:
   *      currency：货币
   *      number： 数字格式
   */
  if (style == "number") {
    // 数字格式
    return parseFloat(value.replace(/[^\d.-]/g, ""));
  } else {
    // 货币格式, 并保留2位小数
    var n = 2;
    value = parseFloat((value + "").replace(/[^\d.-]/g, "")).toFixed(n) + "";
    var l = value
        .split(".")[0]
        .split("")
        .reverse(),
      r = value.split(".")[1];
    var t = "";
    for (var i = 0; i < l.length; i++) {
      t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? "," : "");
    }
    return (
      t
        .split("")
        .reverse()
        .join("") +
      "." +
      r
    );
  }
};

// 日期转换
const date = (value, formatter = "time") => {
  let fmt = formatter;
  if (fmt === "datetime") {
    fmt = "YYYY-MM-DD HH:mm:ss";
  } else if (fmt === "date") {
    fmt = "YYYY-MM-DD";
  } else if (fmt === "time") {
    fmt = "HH:mm:ss";
  }
  return moment(value).format(fmt);
};

const stateText = (value, all) => {
  if (all == null) {
    console.error(`error call stateText(${value}, null)`);
    return "";
  }
  if (!Array.isArray(all)) {
    all = [all];
  }
  const state = all.find(v => v.value == value);
  return state ? state.name : "";
};

export { lower, upper, currencyStyle, date, stateText };
